

package pos.control;

import java.text.ParseException;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import pos.Pos;
import pos.entity.Item;
import pos.storage.db.sql.SQL;
/**
 * This class is a control object that deal with the modify item information operation.
 * @author wei zhang
 */
public class ModifyItemControl {
    /**
     * This method deal with the modify item operation and return a boolean value
     * that indicates if the modification is completed.
     * @param item
     * @param itemid
     * @return
     */
    public boolean modifyItem (Item item, String itemid){
        try{
            String updateQuery;
            PreparedStatement stmt;
            Date dm = null;
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            try {
                dm = df.parse("1/1/1111");
            } catch (ParseException ex) {
                Logger.getLogger(ModifyItemControl.class.getName()).log(Level.SEVERE, null, ex);
            }

            if(!item.getUPCode().equals("")){
                updateQuery = "update items set UPC = \'" + item.getUPCode() + "\' where itemid = "+ itemid;
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            if(!item.getName().equals("")){
                updateQuery = "update items set ItemName = \'" + item.getName() + "\' where itemid = "+ itemid;
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            if(!item.getDesc().equals("")){
                updateQuery = "update items set ItemDescription = \'" + item.getDesc() + "\' where itemid = "+ itemid;
                System.out.println(updateQuery);
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            if(item.getBasePrice() != -1.0){
                updateQuery = "update items set ItemBasePrice = \'" + item.getBasePrice() + "\' where itemid = "+ itemid;
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            if(item.getDateTimeOfManufacture().compareTo(new java.sql.Date(dm.getTime()))== 1){
                updateQuery = "update items set DateTimeOfManufacture = \'" + item.getDateTimeOfManufacture() + "\' where itemid = "+ itemid;
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            if(item.getExpiryDateTime().compareTo(new java.sql.Date(dm.getTime()))== 1){
                updateQuery = "update items set ExpiryDateTime = \'" + item.getExpiryDateTime() + "\' where itemid = "+ itemid;
                stmt = Pos.adminConn.prepareStatement(updateQuery);
                stmt.executeUpdate();
            }
            return true;
        }
        catch(SQLException e){
            e.printStackTrace();
            return false;
        }

    }

}
